package gui;

import java.io.IOException;
import java.util.HashMap;

import sistema.Sistema;

import com.trolltech.qt.gui.QApplication;
import com.trolltech.qt.gui.QHBoxLayout;
import com.trolltech.qt.gui.QIcon;
import com.trolltech.qt.gui.QLabel;
import com.trolltech.qt.gui.QLineEdit;
import com.trolltech.qt.gui.QMessageBox;
import com.trolltech.qt.gui.QPushButton;
import com.trolltech.qt.gui.QTabWidget;
import com.trolltech.qt.gui.QTextEdit;
import com.trolltech.qt.gui.QVBoxLayout;
import com.trolltech.qt.gui.QWidget;

import excecoes.MensagemException;

public class MessengerWidget extends QTabWidget {

	private HashMap<String, QWidget> editedLines = new HashMap<String, QWidget>();
	private Sistema sistema;
	private String userName;
	private final String IMG_PATH = "ImHereM03/img/";
	
	public MessengerWidget(Sistema sistema, String userName) {
		this.userName = userName;
		this.setWindowTitle("Mensageiro");
		this.setWindowIcon(new QIcon(IMG_PATH + "icon.png"));
		this.setStyleSheet("background: url("+IMG_PATH + "background.png);");
		this.sistema = sistema;
		createTabs();
	}

	private void createTabs() {
		this.addTab(createSMSTab(), "SMS");
		this.addTab(createEmailTab(), "E-mail");
	}

	private QWidget createSMSTab() {
		QWidget SMSTab = new QWidget();

		QVBoxLayout vLay = new QVBoxLayout();
		QHBoxLayout hLay = new QHBoxLayout();
		hLay.addLayout(createEditableField("Destinatario:", 50, 2, true));
		hLay.addLayout(createEditableField("Fone", 145, 8, false));
		vLay.addLayout(hLay);
		vLay.addLayout(createEditableField("Remetente:", 200, 20, true));
		QTextEdit text = new QTextEdit();
		this.editedLines.put("textSMS", text);
		vLay.addWidget(new QLabel("Texto:"));
		vLay.addWidget(text);
		vLay.addLayout(createButtons("SMS"));
		SMSTab.setLayout(vLay);

		return SMSTab;
	}

	private QWidget createEmailTab() {
		QWidget SMSTab = new QWidget();

		QVBoxLayout vLay = new QVBoxLayout();
		vLay.addLayout(createEditableField("Para:", 200, 20, true));
		vLay.addLayout(createEditableField("Assunto:", 200, 20, true));
		QTextEdit text = new QTextEdit();
		this.editedLines.put("textEmail", text);
		vLay.addWidget(new QLabel("Texto:"));
		vLay.addWidget(text);
		vLay.addLayout(createButtons("Email"));
		SMSTab.setLayout(vLay);

		return SMSTab;
	}

	private QHBoxLayout createEditableField(String fieldName, int editAreaSize,
			int maxLength, boolean descriptionVisible) {
		QLabel description;
		if (descriptionVisible)
			description = new QLabel(fieldName);
		else
			description = new QLabel("");
		description.setFixedHeight(30);
		description.setFixedWidth(100);
		QLineEdit editArea = new QLineEdit();
		editArea.setFixedHeight(30);
		editArea.setFixedWidth(editAreaSize);
		editArea.setMaxLength(maxLength);
		QHBoxLayout layout = new QHBoxLayout();
		if (!fieldName.equals("Fone"))
			layout.addWidget(description);
		layout.addWidget(editArea);
		this.editedLines.put(fieldName, editArea);

		return layout;
	}

	private QPushButton createButton(String name, String callback) {
		QPushButton button = new QPushButton(name);
		button.clicked.connect(this, callback);
		return button;
	}

	private QHBoxLayout createButtons(String sender) {
		QHBoxLayout hLay = new QHBoxLayout();
		hLay.addWidget(createButton("Enviar", "send" + sender + "()"));
		hLay.addWidget(createButton("Cancelar", "close()"));
		return hLay;
	}

	@SuppressWarnings("unused")
	private void sendSMS() {
		try {
			sistema
					.enviarSMS(
							((QLineEdit) this.editedLines.get("Remetente:"))
									.text(),
							((QLineEdit) this.editedLines.get("Destinatario:"))
									.text()
									+ ((QLineEdit) this.editedLines.get("Fone"))
											.text(),
							((QTextEdit) this.editedLines.get("textSMS"))
									.toPlainText());
		} catch (Exception e) {
			QMessageBox.critical(this, "Erro", e.getMessage());
		}
	}

	@SuppressWarnings("unused")
	private void sendEmail() {

		try {
			sistema.enviarEmail(((QLineEdit) this.editedLines.get("Para:"))
					.text(),
					((QLineEdit) this.editedLines.get("Para:")).text(),
					((QLineEdit) this.editedLines.get("Assunto:")).text(),
					((QTextEdit) this.editedLines.get("textEmail"))
							.toPlainText());
		} catch (Exception e) {
			QMessageBox.critical(this, "Erro", e.getMessage());
		}
	}

//	public static void main(String[] args) {
//		QApplication.initialize(args);
//		MessengerWidget application = new MessengerWidget();
//		application.show();
//		QApplication.exec();
//	}

}
